package cn.edu.sgu.www.mhxysy.service.chongwu.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuJiadianDTO;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuJiadianRenameDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuAttribute;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuJiadian;
import cn.edu.sgu.www.mhxysy.enums.UnlockStatus;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuJiadianMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuJiadianService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Slf4j
@Service
public class ChongwuJiadianServiceImpl implements ChongwuJiadianService {

	private final ChongwuJiadianMapper chongwuJiadianMapper;
	private final ChongwuAttributeMapper chongwuAttributeMapper;

	@Autowired
	public ChongwuJiadianServiceImpl(
			ChongwuJiadianMapper chongwuJiadianMapper,
			ChongwuAttributeMapper chongwuAttributeMapper) {
		this.chongwuJiadianMapper = chongwuJiadianMapper;
		this.chongwuAttributeMapper = chongwuAttributeMapper;
	}

	@Override
	public void rename(ChongwuJiadianRenameDTO chongwuJiadianRenameDTO) {
		UpdateWrapper<ChongwuJiadian> updateWrapper = new UpdateWrapper<>();

		updateWrapper.set("name", chongwuJiadianRenameDTO.getName());

		updateWrapper.eq("chongwu_id", chongwuJiadianRenameDTO.getChongwuId());
		updateWrapper.eq("jiadian_schema", chongwuJiadianRenameDTO.getJiadianSchema());

		/*
		 * update chongwu_jiadian set name = #{name}
		 * where chongwu_id = #{chongwuId} and jiadian_schema = #{jiadianSchema}
		 */
		int update = chongwuJiadianMapper.update(updateWrapper.getEntity(), updateWrapper);

		if (update < 1) {
			log.debug("操作失败，受影响的行数：{}", update);

			throw new GlobalException(ResponseCode.BAD_REQUEST, CustomMessages.OPERATE_FAILURE);
		}
	}

	@Override
	public void unlock(String chongwuId, Integer jiadianSchema) {
		// 通过角色ID和加点方案修改解锁状态
		UpdateWrapper<ChongwuJiadian> updateWrapper = new UpdateWrapper<>();

		updateWrapper.eq("chongwu_id", chongwuId);
		updateWrapper.eq("jiadian_schema", jiadianSchema);
		updateWrapper.eq("unlock_status", UnlockStatus.WJS.getValue());

		updateWrapper.set("unlock_status", UnlockStatus.YJS.getValue());

		int update = chongwuJiadianMapper.update(updateWrapper.getEntity(), updateWrapper);

		if (update != 1) {
			log.debug("操作失败，受影响的行数：{}", update);

			throw new GlobalException(ResponseCode.BAD_REQUEST, "解锁失败，请稍后重试~");
		}
	}

	@Override
	public void update(ChongwuJiadianDTO chongwuJiadianDTO) {
		String chongwuId = chongwuJiadianDTO.getChongwuId();

		QueryWrapper<ChongwuJiadian> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("chongwu_id", chongwuId);
		queryWrapper.eq("jiadian_schema", chongwuJiadianDTO.getJiadianSchema());

		ChongwuJiadian jiadian = chongwuJiadianMapper.selectOne(queryWrapper);

		if (jiadian == null) {
			throw new GlobalException(ResponseCode.BAD_REQUEST, "宠物加点不存在！");
		}

		String jiadianId = jiadian.getId();

		// 1、修改宠物加点
		ChongwuJiadian chongwuJiadian = new ChongwuJiadian();

		chongwuJiadian.setId(jiadianId);
		chongwuJiadian.setMoli(chongwuJiadianDTO.getMoli());
		chongwuJiadian.setTizhi(chongwuJiadianDTO.getTizhi());
		chongwuJiadian.setNaili(chongwuJiadianDTO.getNaili());
		chongwuJiadian.setMinjie(chongwuJiadianDTO.getMinjie());
		chongwuJiadian.setLiliang(chongwuJiadianDTO.getLiliang());

		chongwuJiadianMapper.updateById(chongwuJiadian);

		// 2、修改宠物属性
		ChongwuAttribute chongwuAttribute = new ChongwuAttribute();

		chongwuAttribute.setQixue(chongwuJiadianDTO.getQixue());
		chongwuAttribute.setMofa(chongwuJiadianDTO.getMofa());
		chongwuAttribute.setWlsh(chongwuJiadianDTO.getWlsh());
		chongwuAttribute.setWlfy(chongwuJiadianDTO.getWlfy());
		chongwuAttribute.setFssh(chongwuJiadianDTO.getFssh());
		chongwuAttribute.setFsfy(chongwuJiadianDTO.getFsfy());
		chongwuAttribute.setSudu(chongwuJiadianDTO.getSudu());

		UpdateWrapper<ChongwuAttribute> updateWrapper = new UpdateWrapper<>();

		updateWrapper.eq("chongwu_id", chongwuId);
		updateWrapper.eq("jiadian_id", jiadianId);

		chongwuAttributeMapper.update(chongwuAttribute, updateWrapper);
	}

	@Override
	public ChongwuJiadian selectJiadian(String chongwuId, Integer jiadianSchema) {
		QueryWrapper<ChongwuJiadian> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("jiadian_schema", jiadianSchema);
		queryWrapper.eq("chongwu_id", chongwuId);

		return chongwuJiadianMapper.selectOne(queryWrapper);
	}

}